<?php

require_once( '../system/smarty.inc' );

require_once( '../system/mdao/Class_M_MAP_GROUP_WEEK.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );

require_once( '../system/Class_ERROR.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
require_once( '../system/Class_PWD.php' );

Class_PWD::valid_auth(AOKI_AUTH_MAP_GROUP,$login['staff_auth']);
$isG7  = FALSE;
if   ($login['staff_auth'] == 'G-7') {
	$isG7 = TRUE;
}

$dosearch = FALSE ;

$dao        = new Class_M_MAP_GROUP_WEEK;
$err        = new Class_ERROR;
$matrix_chk = $dao->get_matrix_chk_fields();

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ||
	 isset( $_GET['hist'] ) &&  (  $_GET['hist'] == "2" ) ) {
	$forms =  $_SESSION['MSEARCH']['MAP_GROUP_WEEK'];
}else {
	$forms = $_GET;
	$_SESSION['MSEARCH']['MAP_GROUP_WEEK'] = $forms;
}



$wherearr =  array();
if ( isset( $forms['year4'] ) &&  ( !empty( $forms['year4'] ) ) ){
	$wherearr[] = sprintf("b.year4 =  '%s' ",  $forms['year4'] );
}


if ( isset( $forms['week_cd'] ) &&  ( !empty( $forms['week_cd'] ) ) ){
	$wherearr[] = sprintf(" a.matrix_cd =  '%s' ",  $forms['week_cd'] );
}

if ( isset( $forms['sort'] ) &&  ( !empty( $forms['sort'] ) ) ){
	$wherearr[] = sprintf(" c.sort =  '%s' ",  $forms['sort'] );
}

if ( isset( $forms['map_no'] ) &&  ( !empty( $forms['map_no'] ) ) ){
	$wherearr[] = sprintf(" a.map_no =  '%s' ",  $forms['map_no'] );
}

if ( isset( $forms['shop_cd'] ) &&  ( !empty( $forms['shop_cd'] ) ) ){
	$wherearr[] = sprintf(" a.shop_cd =  '%s' ",  $forms['shop_cd'] );
}


if ( isset( $forms['media_cd'] ) &&  ( !empty( $forms['media_cd'] ) ) ){
	$wherearr[] = sprintf(" c.media_cd = '%s' ", $forms['media_cd'] );
}

if ( isset( $forms['congruent_map'] ) &&  ( $forms['congruent_map'] != '0' ) ){
	$wherearr[] = sprintf(" a.congruent_map =  '%s' ",  $forms['congruent_map'] );
}


if ( isset( $forms['congruent_ins'] ) &&  (  $forms['congruent_ins'] != '0'  ) ){
	$wherearr[] = sprintf(" a.congruent_ins like '%s' ",  "%".$forms['congruent_ins']."%" );
}


if ( isset( $forms['upd_f'] ) &&  ( !empty( $forms['upd_f'] ) ) ){
	$wherearr[] = Class_PWD::get_u_time_where_sql($forms['upd_f'], 1, "a" );
}

if ( isset( $forms['upd_t'] ) &&  ( !empty( $forms['upd_t'] ) ) ){
	$wherearr[] = Class_PWD::get_u_time_where_sql($forms['upd_t'],2, "a" );
}

//---CSVダウンロード　処理開始
if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ){
	$_GET["csv"] = "";
	$tmpfilename = "map_group".date("Ymd_His");
	header("Content-Type: application/octet-stream"); 
	header("Content-Disposition: attachment; filename=$tmpfilename.csv");
	$page = NULL;
	$data = $dao->searchDetails( $wherearr, $page);



	//列名
	$colums = array();
 //1 string code 2 integer 3 date 4 string ""必要 
	$col_heads = array(
		'matrix_cd'       => array( 'マトリックスID',  1),
		'year4'           => array( '年度',            2),
		'week_cd'         => array( '週',              2),
		'flier_date'      => array( '折込日',          3),
		'map_no'          => array( 'マップNO',        1),
		'sort'            => array( '区分',            1),
		'sort_name'       => array( '区分名',          1),
		'media_cd'        => array( 'パターンコード',  1),
		'media_name'      => array( '新聞紙名',          1),
		'shop_cd'         => array( '店舗コード/サービスコード',    1),
		'shop_name'       => array( '店舗名/サービス名', 1),
		'seq'             => array( '順番',              1),
		'congruent_map'   => array( '合同マップ',        1),
		'congruent_ins'   => array( '合同折込',          1),
		'group_no'        => array( 'マップグループNO',1),
		'mark'      	  => array( 'グループNO　親フラグ',1),
		'ins_load_ratio'  => array( '折込負担比率', 1),
		'dummy'           => array( 'ダミー',       1),
		'service'         => array( 'サービス',     1),
		'comm'            => array( 'コメント',     1),
		'memo'            => array( '備考',         1),
		'u_time'          => array( '更新日時',     3),
		'c_time'          => array( '登録日時',     3),
		'm_user_name'     => array( '更新者',       1)
	);
	
	//列名
	foreach ($col_heads as $k=>$v) {
		$colums["name"][]   = $k;
		$colums["head"][]   = $v[0];
		$colums["type"][$k] = $v[1];
	}
	//データ整形開始
	foreach ($data as $k => $v) {
		//
		$data[$k]["flier_date"] =  Class_PWD::int8_to_date($v["flier_date"]);
		$data[$k]["sort_name"]       =  $aoki_map_sort[ $v["sort"] ];
		//$data[$k]["service"]    =  $v["service"]=='1' ? 'サービス ' : '';
		//$data[$k]["congruent_map"]  = $aoki_congruent[$v["congruent_map"]]; 
		//$data[$k]["congruent_ins"]  = $aoki_congruent_ins[$v["congruent_ins"]]; 
		$data[$k]["c_time"]    =  Class_PWD::dateTime_to_YmdHi($v["c_time"]);
		$data[$k]["u_time"]    =  Class_PWD::dateTime_to_YmdHi($v["u_time"]);
	}
	//データ整形完了
	
	$dao->output_csv($data, $colums);
	exit;
}
//---CSVダウンロード　処理完了
if ( isset( $forms['search'] ) && (  !empty( $forms['search'] ) ) ){
	$dosearch = TRUE;
}

if( isset( $forms['page'] ) && is_numeric( $forms['page'] ) ){
	$dosearch = TRUE;
	$page['current'] = $forms['page'];	
}else{
	$page['current'] = 1;
}
$page['item'] = PAGE_MAX;
if ($dosearch) {
	//$page内容は関数内部更新されます。
	$data = $dao->searchDetails( $wherearr, $page );
	
	
	//表示ために整形開始
	$disp = array();
	$old_matrix_cd = "";
	$rows = 0;
	
	foreach ($data as $k => $v ) {
		//$data[$k]["smx"]        =   $aoki_set_flags[$v["smx"]];
		$data[$k]["service"]    =  $v["service"]=='1' ? 'サービス' : '';
		$data[$k]["dummy"]      =  $v["dummy"]=='1' ? 'ダミー' : '';
		
		//$data[$k]["congruent_map"]  = $aoki_congruent[$v["congruent_map"]]; 
		//$data[$k]["congruent_ins"]  = $aoki_congruent_ins[$v["congruent_ins"]]; 
		if ($old_matrix_cd != $v["matrix_cd"]) {
			if (!empty($old_matrix_cd)) {
				$disp[] = $rows;
			}
			$data[$k]["rowspan"] = 1 ;
			$data[$k]["flier_date"]  = Class_PWD::dateTime_to_dateD( $v["flier_date"] );
			$rows = 1;
			$old_matrix_cd = $v["matrix_cd"];
		} else {
			$rows = $rows + 1;
			$data[$k]["rowspan"] = 0 ;
		}
	}
	$disp[] = $rows;
	$index = 0;
	foreach ($data as $k => $v ) {
		if ($data[$k]["rowspan"] == 1 ) {
			
			$data[$k]["rowspannums"] = $disp[$index];
			if  (($index + 1) % 2 ==0) {
				$data[$k]["rowspanclass"] = "mod" ;
			}else {
				$data[$k]["rowspanclass"] = "odd" ;
			}
			$index = $index + 1;
		}
	}
	
	
	//表示ために整形完了
		
	
}

if (empty($forms["year4"])) {
	$forms["year4"] = date('Y');
}

if( isset($forms) )       $smarty->assign('forms', $forms);
if( isset($data) )        $smarty->assign('data', $data);
if( isset($page) )        $smarty->assign('page', $page);
if( isset($err_mes ) )    $smarty->assign('err', $err_mes);
if( isset($sqlmessage ) ) $smarty->assign('errcopy', $sqlmessage);
if( isset($sqldelmsg ) )  $smarty->assign('errdel', $sqldelmsg);
$smarty->assign('isG7', $isG7 );
$smarty->assign('year4',  Class_PWD::get_years());

$week= Class_M_OTHER::get_week_array( $forms["year4"], PLEASE_SELECT_ARR );
$smarty->assign('week', $week );

$map_sort[""] = "すべて";
foreach ($aoki_map_sort as $k => $v) {
	$map_sort[$k] = $v;
}
$smarty->assign('map_sort', $map_sort );

$map_congruent["0"] = "すべて";
foreach ($aoki_congruent as $k => $v) {
	$map_congruent[$k] = $v;
}
$smarty->assign('congruent',     $map_congruent );

$map_ins["0"] = "すべて";
foreach ($aoki_congruent_ins as $k => $v) {
	$map_ins[$k] = $v;
}

$pattern = Class_M_OTHER::get_pattern_array(PLEASE_SELECT_ARR, 23);
$smarty->assign('pattern', $pattern );
$smarty->assign('congruent_ins', $map_ins );

$smarty->assign('menu',  $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );
$smarty->assign('pageing', $smarty->fetch('page.html') );
$smarty->display('mmapg_index.html');

?>